package com.zqh.volatileDemo;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 曾庆红
 * {@code @date} 2023-10-12 22:17
 * reentrantLock替代sychronized
 */

public class T3 {
    Lock lock = new ReentrantLock();
    void m1(){
        try{
            lock.lock();
            for (int i = 0; i<10; i++){
                TimeUnit.SECONDS.sleep(1);
                System.out.println(i);
            }
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    void m2(){
        try{
            lock.lock();
            System.out.println("m2 ...");
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        T3 t = new T3();
        new Thread(t::m1).start();
        try{
            TimeUnit.SECONDS.sleep(1);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        new Thread(t::m1).start();
    }
}
